<?php

namespace app\model;

use app\controller\wmapi\IndexController;
use support\Db;
use support\Model;

class WaChannel
{
    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'wa_channel';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id';

    /**
     * Indicates if the model should be timestamped.
     *
     * @var bool
     */
    public $timestamps = false;

    /**
     * 角色列表 - 分页
     * @return false|string
     */
    public static function list($page,$pageNum,$whereIn,$fields,$condition)
    {

        $offset = ($page - 1) * $pageNum;
        $model = Db::table('wa_channel')
            ->select($fields)
            ->where('status', '!=', 4)
            ->where($condition)
            ->whereIn('admin_id',$whereIn);
        $count = $model->where($condition)->count();
        $list = $model
            ->orderBy('id', 'desc')
            ->offset($offset)
            ->limit($pageNum)
            ->get();
//        ->paginate($pageNum);
        foreach ($list as $k) {
            //,维护人(admin_id)
            $maintainer_admin_name = ''; // 维护人名称
            $maintainer_department_name = '';  // 维护人部门名称
            $created_name = ''; // 创建人名称
            $created_department_name = ''; // 创建人部门名称
            if ($k->maintainer_admin_id) {
                $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                $admins = json_decode($admins);
                if ($admins) {
                    $maintainer_admin_name = $admins->nickname;
                    if ($admins->department_id){
                        $department = WaDepartment::find(['id' => intval($admins->department_id)]);
                        $department = json_decode($department);
                        if ($department){
                            $maintainer_department_name = $department->name;  // 维护人部门名称
                        }
                    }
                }

            }
            // 创建人
            if ($k->created_id) {
                $admins = WaAdmins::find(['id' => intval($k->created_id)]);
                $admins = json_decode($admins);
                if ($admins) {
                    $created_name = $admins->nickname;
                    if ($admins->department_id){
                        $department = WaDepartment::find(['id' => intval($admins->department_id)]);
                        $department = json_decode($department);
                        if ($department){
                            $created_department_name = $department->name;  // 创建人部门名称
                        }
                    }
                }
            }

            // 团队
            if ($k->term_id) {
                $terms = WaDepartment::find(['id' => intval($k->term_id)]);
                $terms = json_decode($terms);
                if ($terms) {
                    $term_name = $terms->name;
                } else {
                    $term_name = '';
                }
            } else {
                $term_name = '';
            }
            // 1 个人 2 企业
            if ($k->type == 1) {
                $k->type_name = '个人';
            }elseif ($k->type == 2){
                $k->type_name = '企业';
            }else{
                $k->type_name = '';
            }
            $k->maintainer_admin_name = $maintainer_admin_name;// 维护人名称
            $k->maintainer_department_name = $maintainer_department_name;// 维护人部门名称
            $k->created_name = $created_name; // 创建人名称
            $k->created_department_name = $created_department_name; // 创建人部门名称
            $k->term_name = $term_name;
        }
        return json_encode(['list' => $list, 'count' => $count]);
    }


    /**
     * 角色列表 - 无分页
     * @param $page
     * @param $pageNum
     * @param $fields
     * @param $condition
     * @return false|string
     */
    public static function all($fields,$condition)
    {

        $list = Db::table('wa_channel')
            ->select($fields)
            ->where('status', '!=', 4)
            ->where($condition)
            ->orderBy('id', 'asc')
            ->get();
        foreach ($list as $k) {
            //,维护人(admin_id)
            if ($k->maintainer_admin_id) {
                $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                $admins = json_decode($admins);
                if ($admins) {
                    $maintainer_admin_name = $admins->nickname;
                } else {
                    $maintainer_admin_name = '';
                }

            } else {
                $maintainer_admin_name = '';
            }
            // 创建人
            if ($k->created_id) {
                $admins = WaAdmins::find(['id' => intval($k->created_id)]);
                $admins = json_decode($admins);
                if ($admins) {
                    $created_name = $admins->nickname;
                } else {
                    $created_name = '';
                }
            } else {
                $created_name = '';
            }

            // 团队
            if ($k->term_id) {
                $terms = WaDepartment::find(['id' => intval($k->term_id)]);
                $terms = json_decode($terms);
                if ($terms) {
                    $term_name = $terms->name;
                } else {
                    $term_name = '';
                }
            } else {
                $term_name = '';
            }
            $k->maintainer_admin_name = $maintainer_admin_name;
            $k->created_name = $created_name;
            $k->term_name = $term_name;
        }
        return json_encode($list);
    }


    public static function find($condition)
    {
        $res = Db::table('wa_channel')
            ->where($condition)
            ->first();
        if ($res){
            foreach (array($res) as $k) {
                //,维护人(admin_id)
                if ($k->maintainer_admin_id) {
                    $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $maintainer_admin_name = $admins->nickname;
                    } else {
                        $maintainer_admin_name = '';
                    }

                } else {
                    $maintainer_admin_name = '';
                }
                // 创建人
                if ($k->created_id) {
                    $admins = WaAdmins::find(['id' => intval($k->maintainer_admin_id)]);
                    $admins = json_decode($admins);
                    if ($admins) {
                        $created_name = $admins->nickname;
                    } else {
                        $created_name = '';
                    }
                } else {
                    $created_name = '';
                }

                // 团队/部门
                if ($k->term_id) {
                    $terms = WaDepartment::find(['id' => intval($k->term_id)]);
                    $terms = json_decode($terms);
                    if ($terms) {
                        $term_name = $terms->name;
                    } else {
                        $term_name = '';
                    }
                } else {
                    $term_name = '';
                }
                // 1 个人 2 企业
                if ($k->type == 1) {
                    $k->type_name = '个人';
                }elseif ($k->type == 2){
                    $k->type_name = '企业';
                }else{
                    $k->type_name = '';
                }


                $res->maintainer_admin_name = $maintainer_admin_name;
                $res->created_name = $created_name;
                $res->term_name = $term_name;
            }
        }

        return json_encode($res);
    }

//    public static function find($condition)
//    {
//        $res = Db::table('wa_channel')
//            ->where($condition)
//            ->first();
//        return json_encode($res);
//    }

    public static function created($data){

        return Db::table('wa_channel')->insertGetId($data);

    }

    public static function updated($condition,$data){

        $res = Db::table('wa_channel')->where($condition)->update($data);
        return json_encode($res);

    }

    public static function deleted($condition){

        $res = Db::table('wa_channel')->where($condition)->delete();
        return json_encode($res);

    }



}